package com.tensquare.recruit.controller;

import com.tensquare.recruit.pojo.Enterprise;
import com.tensquare.recruit.service.EnterpriseService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:zimmor
 * @Date: 2021/3/1 9:30
 * @Description 企业控制层
 * @version: 1.0.0
 */

@RestController
@CrossOrigin
@RequestMapping("/enterprise")
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 查询热门企业
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/search/hotlist")
    public Result isHotList() {
        return new Result(true, StatusCode.OK, "查询成功", enterpriseService.ishot("1"));
    }

    /**
     * 分页查询
     * @param enterprise
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value="/search/{page}/{size}")
    public Result findSearch(@RequestBody Enterprise enterprise, @PathVariable int page, @PathVariable int size) {
        Page<Enterprise> pageData = enterpriseService.findSearch(enterprise, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Enterprise>(pageData.getTotalElements(),pageData.getContent()));
    }

    /**
     * 条件查询
     * @param enterprise
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/search")
    public Result findSearch(@RequestBody Enterprise enterprise) {
        return new Result(true, StatusCode.OK, "查询成功", enterpriseService.findSearch(enterprise));
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public Result findById(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "查询成功", enterpriseService.findById(id));
    }

    /**
     * 修改
     * @param enterprise
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/{id}")
    public Result update(@RequestBody Enterprise enterprise, @PathVariable String id) {
        enterprise.setId(id);
        enterpriseService.update(enterprise);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public Result deleteById(@PathVariable String id) {
        enterpriseService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
